package com.demon.androidbasic.widget

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.RawRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import com.airbnb.lottie.LottieAnimationView
import com.demon.androidbasic.R

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2019/04/18
 * desc   : 状态布局（网络错误，异常错误，空数据）
 */
class HintLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) {
    /** 提示布局  */
    private var mMainLayout: ViewGroup? = null

    /** 提示图标  */
    private var mImageView: LottieAnimationView? = null

    /** 提示文本  */
    private var mTextView: TextView? = null

    /**
     * 显示
     */
    fun show() {
        if (mMainLayout == null) {
            //初始化布局
            initLayout()
        }
        if (!isShow) {
            // 显示布局
            mMainLayout!!.visibility = VISIBLE
        }
    }

    /**
     * 隐藏
     */
    fun hide() {
        if (mMainLayout != null && isShow) {
            //隐藏布局
            mMainLayout!!.visibility = INVISIBLE
        }
    }

    /**
     * 是否显示了
     */
    val isShow: Boolean
        get() = mMainLayout != null && mMainLayout!!.visibility == VISIBLE

    /**
     * 设置提示图标，请在show方法之后调用
     */
    fun setIcon(@DrawableRes id: Int) {
        setIcon(ContextCompat.getDrawable(context, id))
    }

    fun setIcon(drawable: Drawable?) {
        if (mImageView != null) {
            mImageView!!.setImageDrawable(drawable)
        }
    }

    /**
     * 设置提示动画
     */
    fun setAnim(@RawRes id: Int) {
        mImageView!!.setAnimation(id)
        // 这里需要调用播放动画，否则会出现第一次显示动画效果正常，第二次显示动画会不动
        mImageView!!.playAnimation()
    }

    /**
     * 设置提示文本，请在show方法之后调用
     */
    fun setHint(@StringRes id: Int) {
        setHint(resources.getString(id))
    }

    fun setHint(text: CharSequence?) {
        if (mTextView != null && text != null) {
            mTextView!!.text = text
        }
    }

    /**
     * 初始化提示的布局
     */
    private fun initLayout() {
        mMainLayout = LayoutInflater.from(context)
            .inflate(R.layout.widget_hint_layout, this, false) as ViewGroup
        mImageView = mMainLayout!!.findViewById(R.id.iv_hint_icon)
        mTextView = mMainLayout!!.findViewById(R.id.iv_hint_text)
        if (mMainLayout!!.background == null) {
            // 默认使用 windowBackground 作为背景
            val ta = context.obtainStyledAttributes(intArrayOf(android.R.attr.windowBackground))
            mMainLayout!!.background = ta.getDrawable(0)
            ta.recycle()
        }
        addView(mMainLayout)
    }

    override fun setOnClickListener(l: OnClickListener?) {
        if (isShow) {
            mMainLayout!!.setOnClickListener(l)
        } else {
            super.setOnClickListener(l)
        }
    }

    init {
        isClickable = true
        isFocusable = true
        isFocusableInTouchMode = true
    }
}